Remove backslash before open and close braces

I am trying to figure out a way to create a script that will remove backslashes that appear in front of open and close braces ({ }).  I am trying to use another piece of code as a template, but can't figure out how to alter the code to do what I want.  I know I'll need a while loop within the function, but I'm not sure how to set up this while loop.  This is what I've come up with so far:

string removeBackslashBraces(string s) {
    Buffer str = create;
    str = s;
    int len = length s;
    int j, i=0;
    Regexp openBrace = regexp("\\{");
    Regexp closeBrace = regexp ("\\}");
    
    while (search(openBrace, str, 0) || search(closeBrace, str, 0)) {
        if (search(openBrace, str, 0)) {
            for (j=len-1; j>=0; j--) {
                if (s[j]=="{" && s[j-1]=="\\") {
                    i=j-1;
                    break;
                }
            }
            s = str[0:i-1] str[i+1:];
        } // end if
        else if (search(closeBrace, str, 0)) {
            for (j=len-1; j>=0; j--) {
                if (s[j]=="}" && s[j-1]=="\\") {
                    i = j-1;
                    break;
                }
            }
        }
        s = str[0:i-1] str[i+1:length str];
        str = s
    }
    return stringOf str;
}

Can someone please help me figure this function out?

 

Chris


Christopher Cote - Thu Mar 21 09:41:12 EDT 2019

Re: Remove backslash before open and close braces
llandale - Thu Mar 21 17:12:57 EDT 2019

I don't have both Internet and DOORS available at the same time, so I write this without testing.

  • Move line 4 "len = length(s)" inside the loop after line #9 and change it to "len = length(str)".  The length of the buffer changes with each iteration.
  • Line #27 needs moved up after line #25, and indented.
  • Turn line #30 into "string Result = stringOf(str); delete(str); return Result".  I see you are creating the buffer but not deleting it resulting in Allocated Units accumulating in the program.
  • If you keep your RegExp, I would move the two static declarations outside the function as private global variables.  Creating these inside the loop results in you never releasing the allocated units associated with them.  Perhaps named "removeBackslashBraces_Open" and "removeBackslashBraces_Close"
  • Lines 11 and 20 should change "j >=0" to "j > 0".  You don't want j-=0 and subsequently try to get character j-1.
  • Actually, you don't need to search, there is a method of finding out where the regexp matched (but I don't recall what it is).
  • You need only one Reqexp : regexp("\\[{}]"  // backslash followed by open or close brace
  • I would prefer two separate while loops; one for each regexp.
  • I would actually get rid of lines #9, 10, and 19; don't bother with regexp at all.  Replace with "Done = false"; while(!Done); { Done = true; i = -1.  Preceed line 17 and 27 with "if (I >= 0) and follow that with "Done = false"

Maybe that will get you over the hump.

-Louie